package com.vitas.coin.ui.fm

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.snap.base.util.BaseUtil
import com.snap.common.main.BaseMVVMFg
import com.vitas.coin.R
import com.vitas.coin.callback.MusicAdapterCallback
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.event.CollectEvent
import com.vitas.coin.data.event.PausePlayEvent
import com.vitas.coin.data.event.RequestFinishEvent
import com.vitas.coin.data.event.StartPlayEvent
import com.vitas.coin.databinding.FrgMusicBinding
import com.vitas.coin.util.Audio
import com.vitas.coin.vm.HomeVM


class MusicFrg : BaseMVVMFg<FrgMusicBinding, HomeVM>() {


    companion object {
        fun newInstance(
            name: String?,
            parentId: Long?
        ): MusicFrg {
            val fragment = MusicFrg()
            val bundle = Bundle()
            bundle.putString(INTENT_NAME, name)
            if (parentId != null) {
                bundle.putLong(PARENT_ID, parentId)
            }
            fragment.setArguments(bundle)
            return fragment
        }

        const val INTENT_NAME = "NAME"
        const val PARENT_ID = "PARENT_ID"
    }

    override fun createVM(): HomeVM {
        return HomeVM().apply {
            actionAct = { requireActivity() }
        }
    }

    var pageNumber: Int = 1

    var parentId: Long = 1


    override fun doDataBind() {
        binding.viewModel = viewModel
    }

    override fun getContentViewId(): Int {
        return R.layout.frg_music
    }

    override fun onViewCreated() {
        initRv()
        initRefreshLayout()
        initType()
        initObserve()
    }

    private fun initType() {
        val mBundle = arguments ?: return
        parentId = mBundle.getLong(PARENT_ID)
        val cateName = mBundle.getString(INTENT_NAME)
        if (cateName != null) {
            viewModel.setCateName(cateName)
        }
        viewModel.loadData(parentId, pageNumber)
    }

    private fun initRefreshLayout() {
        binding.mRefreshLayout.setRefreshHeader(ClassicsHeader(context))
        binding.mRefreshLayout.setRefreshFooter(ClassicsFooter(context))
        binding.mRefreshLayout.setEnableRefresh(true)
        binding.mRefreshLayout.setEnableLoadMore(true)
        binding.mRefreshLayout.setOnRefreshListener {
            pageNumber = 1
            viewModel.loadData(parentId, pageNumber)
        }
        binding.mRefreshLayout.setOnLoadMoreListener {
            pageNumber++
            viewModel.loadData(parentId, pageNumber)
        }
    }


    private fun initObserve() {
        BaseUtil.observer(this, StartPlayEvent::class.java) {
            it.musicObj.id?.let { it1 -> viewModel.freshListByPlay(it1, true) }
        }

        BaseUtil.observer(this, PausePlayEvent::class.java) {
            it.musicObj.id?.let { it1 -> viewModel.freshListByPlay(it1, false) }
        }


        BaseUtil.observer(this, CollectEvent::class.java) {
            viewModel.freshListByCollect(it.musicObj)
        }

        BaseUtil.observer(this, RequestFinishEvent::class.java) {
            binding.mRefreshLayout.finishRefresh(500)
            binding.mRefreshLayout.finishLoadMore()
        }
    }


    private fun initRv() {
        binding.mRecyclerView.layoutManager =
            LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.mRecyclerView.adapter = viewModel.musicAdapter
        viewModel.musicAdapter.setDownloadCallBack(object :
            MusicAdapterCallback {
            override fun clickDownload(mChildrenVoiceBean: ChildrenVoiceBean?, type: Int) {
                if (mChildrenVoiceBean != null) {
                    viewModel.download(mChildrenVoiceBean, type, editAct = { name, path ->
                        run {
                            Audio.openEdit(requireActivity(), path, name)
                        }
                    })
                }
            }

            override fun clickCollect(mChildrenVoiceBean: ChildrenVoiceBean?) {
                if (mChildrenVoiceBean != null) {
                    viewModel.collect(mChildrenVoiceBean)
                }
            }
        })
        viewModel.musicAdapter.setType(1)
    }


}